package com.example.demo;

import org.junit.jupiter.api.Test;

import java.sql.*;

public class jdbc {
void test2() throws ClassNotFoundException,SQLException{
   this.save("hzy","19201170","21");

}

    private void save(String name, String idno, String age) throws ClassNotFoundException, SQLException {
        String sql="SELECT *FROM t_student where name =?";
        System.out.println("sql:"+sql);
       // String sql="INSERT INTO t_student (name, idno, age) VALUES ('"+name+"','"+idno+"',"+age+")";
        String URL = "jdbc:mysql://192.168.80.64:3306/soft_test?characterEncoding=utf-8";
        String USER = "root";
        String PASSWORD = "123456";
        // 1.加载驱动程序
        Class.forName("com.mysql.jdbc.Driver");
        // 2.获得数据库链接
        try(Connection conn = DriverManager.getConnection(URL, USER, PASSWORD)) {
            PreparedStatement preparedStatement=conn.prepareStatement(sql);
            preparedStatement.setString(1,name);
            preparedStatement.executeQuery();


    }
/**
    @Test
    void test() throws ClassNotFoundException,SQLException {
        String URL = "jdbc:mysql://192.168.80.64:3306/soft_test?characterEncoding=utf-8";
        String USER = "root";
        String PASSWORD = "123456";
        // 1.加载驱动程序
        Class.forName("com.mysql.jdbc.Driver");
        // 2.获得数据库链接
        try(Connection conn = DriverManager.getConnection(URL, USER, PASSWORD)) {
            System.out.println(conn.isClosed());
            String sql="INSERT INTO t_student";
            try( Statement statement=conn.createStatement()){
                boolean execute=statement.execute(sql);
                System.out.println(execute);

                try(ResultSet resultSet=statement.executeQuery("Select * FROH t_student WHERE id=2")) {
                    while (resultSet.next()) {
                        resultSet.getInt("id");
                        String name = resultSet.getString("name");
                        String idno = resultSet.getString("idno");
                        int age=resultSet.getInt("age");
                        System.out.printf(",name=%s,idno=%s,age=%age\n",name,idno,age);
                    }
                }*/

/**
        String URL = "jdbc:mysql://192.168.80.64:3306/soft_test?characterEncoding=utf-8";
        String USER = "root";
        String PASSWORD = "123456";
        // 1.加载驱动程序
        Class.forName("com.mysql.jdbc.Driver");
        // 2.获得数据库链接
           try(Connection conn = DriverManager.getConnection(URL, USER, PASSWORD)) {
            System.out.println(conn.isClosed());

             String sql="INSERT INTO t_student (name, idno, age) VALUES ('黄哲宇','190201170','11')";
          try( Statement statement=conn.createStatement()){
              boolean execute=statement.execute(sql);
              System.out.println(execute);

              try(ResultSet resultSet=statement.executeQuery("Select * FROH t_student WHERE id=2")) {
                  while (resultSet.next()) {
                      resultSet.getInt("id");
                      String name = resultSet.getString("name");
                      String idno = resultSet.getString("idno");
                      int age=resultSet.getInt("age");
                      System.out.printf(",name=%s,idno=%s,age=%age\n",name,idno,age);
                  }
              }
          }**/
  /**
Connection conn=null;
try {
    conn=DriverManager.getConnection();

}
finally {
    if(conn!=null){
        conn.close();
    }
}*/
         }
}

            // 3.通过数据库的连接操作数据库，实现增删改查（使用Statement类）
            // String name="张三";
            //预编译
            //   String sql="select * from userinfo where UserName=?";
            //   PreparedStatement statement = conn.prepareStatement(sql);
            //   statement.setString(1, name);
            //  ResultSet rs = statement.executeQuery();
//			String sql="select * from userinfo where UserName='"+name+"'";
//			Statement statement = conn.createStatement();
//			ResultSet rs = statement.executeQuery(sql);
            // 4.处理数据库的返回结果(使用ResultSet类)
            //   while (rs.next()) {
            //      System.out.println(rs.getString("UserName") + " " + rs.getString("Password"));
            // }

            // 关闭资源
            //       conn.close();
            //      rs.close();
            //     statement.close();
            // } catch (ClassNotFoundException e) {
            //   e.printStackTrace();
            //}catch (SQLException e) {
            //  e.printStackTrace();
            //}



